package awesome.site.proprietor;

import java.io.File;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;

import awesome.site.loader.util.CheckSum;

/**
 * Holds a web object (web page, image, etc) in memory.
 * 
 * @author Bee Vo
 * 
 */
public class WebObjectProprietor {
	private String ID, webFileName, mimeType;
	private byte[] webFile;

	/**
	 * Constructor for a WebObjectProprietor, holds web files in memory.
	 * @param toLoad file to be made into an object
	 */
	public WebObjectProprietor(File toLoad){
		try {
			this.webFile = Files.readAllBytes(toLoad.toPath());
			this.webFileName = toLoad.toPath().toString();
			this.ID = CheckSum.getCheckSum(webFile);
			this.mimeType = determineFileType(toLoad);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String determineFileType(File toLoad) {
		FileNameMap fileNameMap = URLConnection.getFileNameMap();
		return fileNameMap.getContentTypeFor(toLoad.getAbsolutePath());
	}

	public String getID() {
		return ID;
	}

	public byte[] getWebFile() {
		return webFile;
	}

	public String getWebFileName() {
		return webFileName;
	}

	public String getFileType() {
		return mimeType;
	}
}
